//
//  TuiKuanDetailViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/9/21.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "TuiKuanDetailViewController.h"
#import "TuiKuanDetailTableViewCell.h"
#import "TuiKuanDetailSecondTableViewCell.h"

@interface TuiKuanDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *tuiKuanDic;

@end

@implementation TuiKuanDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWhite;
    [self loadBaseTitleLable:@"详情"];
    
    [self setUpUI];
    
    [self getTuiKuanDetail];
}
#pragma mark - 退款详情
- (void)getTuiKuanDetail
{
    if (self.cartid && self.commodityid) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/aftersale.ashx?action=info&companyid=%@&code=%@&timestamp=%@&cartid=%@&commodityid=%@&from=mobile",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp],self.cartid,self.commodityid] parameters:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            
            self.tuiKuanDic = [NSDictionary dictionary];
            
            if ([responseObject[@"status"] intValue] == 1) {
                self.tuiKuanDic = responseObject;
                [self.tableView reloadData];
            } else {
                [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
            }
            
        } failure:^(NSError *error) {
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
            
        }];
    }
}
- (void)setUpUI
{
    [self.view addSubview:self.tableView];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 8;
    } else {
        if (self.tuiKuanDic[@"repairsImages"]) {
            NSArray *array = [self.tuiKuanDic[@"repairsImages"] componentsSeparatedByString:@","];
            NSMutableArray *array1 = [NSMutableArray array];
            for (NSString *str in array) {
                if (str.length != 0) {
                    [array1 addObject:str];
                }
            }
            return array1.count;
        } else {
            return 0;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        TuiKuanDetailTableViewCell *cell = [[TuiKuanDetailTableViewCell alloc] init];
        cell.titleLB.text = [NSString stringWithFormat:@"%@",self.tuiKuanDic[@"CommodityName"]];
        [cell.goodsImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",self.tuiKuanDic[@"CartImg"]]] placeholderImage:UIImageNamed(@"placeholder")];
        cell.priceLB.text = [NSString stringWithFormat:@"规格：%@",self.tuiKuanDic[@"CommodityProperty"]];
        cell.numLB.text = [NSString stringWithFormat:@"数量：×%@",self.tuiKuanDic[@"repairsNumber"]];
        cell.timeLB.text = [NSString stringWithFormat:@"购买时间：%@",self.tuiKuanDic[@"orderDate"]];
        
        cell.selectionStyle = NO;
        return cell;
    } else if (indexPath.section == 1) {
        TuiKuanDetailSecondTableViewCell *cell = [[TuiKuanDetailSecondTableViewCell alloc] init];
        if (indexPath.row == 0) {
            cell.leftLB.text = [NSString stringWithFormat:@"服务单号：%@",self.tuiKuanDic[@"repairsId"]];
        } else if (indexPath.row == 1) {
            cell.leftLB.text = [NSString stringWithFormat:@"申请时间：%@",self.tuiKuanDic[@"repairsCreatTime"]];
        } else if (indexPath.row == 2) {
            if ([self.tuiKuanDic[@"repairsType"] intValue] == 1) {
                cell.leftLB.text = @"服务类型：退货";
            } else if ([self.tuiKuanDic[@"repairsType"] intValue] == 2) {
                cell.leftLB.text = @"服务类型：换货";
            } else if ([self.tuiKuanDic[@"repairsType"] intValue] == 3) {
                cell.leftLB.text = @"服务类型：维修";
            }
            
            cell.rightLB.hidden = NO;
            if ([self.tuiKuanDic[@"repairsProof"] intValue] == 1) {
                cell.rightLB.text = @"申请票据：发票信息";
            } else if ([self.tuiKuanDic[@"repairsType"] intValue] == 2) {
                cell.rightLB.text = @"申请票据：检测报告";
            } else if ([self.tuiKuanDic[@"repairsType"] intValue] == 3) {
                cell.rightLB.text = @"申请票据：其他文件";
            }
            
        } else if (indexPath.row == 3) {
            cell.leftLB.text = [NSString stringWithFormat:@"联系人：%@",self.tuiKuanDic[@"repairsName"]];
            cell.rightLB.hidden = NO;
            cell.rightLB.text = [NSString stringWithFormat:@"手机号码：%@",self.tuiKuanDic[@"repairsPhone"]];
        } else if (indexPath.row == 4) {
            cell.leftLB.text = @"收货地址：";
        } else if (indexPath.row == 5) {
            cell.leftLB.text = [NSString stringWithFormat:@"%@%@%@%@",self.tuiKuanDic[@"addressProvince"],self.tuiKuanDic[@"addressCity"],self.tuiKuanDic[@"addressArea"],self.tuiKuanDic[@"addressXX"]];
        } else if (indexPath.row == 6) {
            cell.leftLB.text = @"问题描述：";
        } else if (indexPath.row == 7) {
            cell.leftLB.text = self.tuiKuanDic[@"repairsContent"];
        }
        cell.selectionStyle = NO;
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        UIImageView *img = [[UIImageView alloc] init];
        [cell.contentView addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_offset(CGSizeMake(kScreenWidth-rateWidth(20), rateHeight(690)));
            make.left.equalTo(cell.contentView).offset(rateWidth(10));
            make.top.equalTo(cell.contentView).offset(rateHeight(10));
        }];
        img.contentMode = UIViewContentModeScaleAspectFit;
        // 赋值
        if (self.tuiKuanDic[@"repairsImages"]) {
            NSArray *array = [self.tuiKuanDic[@"repairsImages"] componentsSeparatedByString:@","];
            NSMutableArray *array1 = [NSMutableArray array];
            for (NSString *str in array) {
                if (str.length != 0) {
                    [array1 addObject:str];
                }
            }
            [img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",array1[indexPath.row]]] placeholderImage:UIImageNamed(@"placeholder")];
        }
        cell.selectionStyle = NO;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return rateHeight(320);
    } else if (indexPath.section == 1) {
        return rateHeight(90);
    } else {
        return rateHeight(700);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return rateHeight(90);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = tableViewBgColor;
    
    UILabel *label1 = [UILabel labelWithText:@"" textColor:UIColorFromRGB(0x666666) fontSize:13];
    [label1 sizeToFit];
    [bgV addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgV);
        make.left.equalTo(bgV).offset(rateWidth(30));
    }];
    UILabel *label2 = [UILabel labelWithText:@"" textColor:UIColorFromRGB(0x666666) fontSize:13];
    [label2 sizeToFit];
    [bgV addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgV);
        make.right.equalTo(bgV).offset(-rateWidth(30));
    }];
    if (section == 0) {
        label1.text = [NSString stringWithFormat:@"订单号：%@",self.orderId];
        if ([self.tuiKuanDic[@"repairsStatus"] intValue] == 0) {
            label2.text = @"待审核";
        } else if ([self.tuiKuanDic[@"repairsStatus"] intValue] == 1) {
            label2.text = @"已驳回";
        } else if ([self.tuiKuanDic[@"repairsStatus"] intValue] == 2) {
            label2.text = @"待邮寄";
        } else if ([self.tuiKuanDic[@"repairsStatus"] intValue] == 3) {
            label2.text = @"已收货";
        } else if ([self.tuiKuanDic[@"repairsStatus"] intValue] == 4) {
            label2.text = @"处理中";
        } else if ([self.tuiKuanDic[@"repairsStatus"] intValue] == 5) {
            label2.text = @"已完成";
        }
        
    } else if (section == 1) {
        label1.text = @"售后单详情";
    } else if (section == 2) {
        label1.text = @"图片信息";
    }
    
    return bgV;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeight-SafeAreaTopHeight) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = tableViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
